package org.edu.controller;

import java.io.Serializable;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.ListDataModel;
import org.edu.model.Notesl;
import org.edu.model.StudentLesson;
import org.edu.model.TeacherLesson;
import org.edu.model.User;
import org.edu.service.StudentLessonService;
import org.primefaces.model.SelectableDataModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

@Controller
@SessionScoped
public class StudentLessonController extends ListDataModel<StudentLesson> implements Serializable, SelectableDataModel<StudentLesson> {

	private static final long serialVersionUID = 1L;
	@Autowired
	private transient StudentLessonService studentLessonService;

	private StudentLesson studentLesson;
	private TeacherLesson teacherLesson;
	private Notesl notesl;
	private List<StudentLesson> studentLessons;
	private List<StudentLesson> filterStudentLesson;
	private List<TeacherLesson> listTeacherLessons;
	private List<StudentLesson> myLessonList;
	
	private User user;
	private Boolean getLesson;
	
	public StudentLessonController() {
		studentLesson = new StudentLesson();
		notesl = new Notesl();
	}

	@PostConstruct
	public void init() throws Exception {
		studentLessons = studentLessonService.studentLessons();
//		myLessonList = studentLessonService.myLessonList();
	}

	public void preparedAddingLesson(){
		studentLesson = new StudentLesson();
		notesl = new Notesl();
	}
	
	public String save() {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		try {
			studentLesson.setStatus_lesson(false);
			studentLesson.setChoose_lesson(true);
			studentLesson.setNotesl(notesl);
			notesl.setStudentLesson(studentLesson);
			studentLessonService.save(studentLesson);
			facesContext.addMessage(null, new FacesMessage("Save Teacher Lesson is Succesful"));
			init();
			studentLesson = new StudentLesson();
			notesl = new Notesl();
		} catch (Exception e) {
			facesContext.addMessage(null, new FacesMessage("Not Succesful"));
			e.printStackTrace();
		}
		return null;
	}
	

	public StudentLesson getStudentLesson() {
		if(studentLesson == null){
			studentLesson = new StudentLesson();
		}
		return studentLesson;
	}

	public void setStudentLesson(StudentLesson studentLesson) {
		this.studentLesson = studentLesson;
	}

	public List<StudentLesson> getStudentLessons() {
		return studentLessons;
	}

	public List<StudentLesson> getFilterStudentLesson() {
		return filterStudentLesson;
	}

	public void setFilterStudentLesson(List<StudentLesson> filterStudentLesson) {
		this.filterStudentLesson = filterStudentLesson;
	}
	
	public Notesl getNotesl() {
		if(notesl == null){
			notesl = new Notesl();
		}
		return notesl;
	}

	public void setNotesl(Notesl notesl) {
		this.notesl = notesl;
	}

	public TeacherLesson getTeacherLesson() {
		if(teacherLesson == null){
			teacherLesson = new TeacherLesson();
		}
		return teacherLesson;
	}

	public void setTeacherLesson(TeacherLesson teacherLesson) {
		this.teacherLesson = teacherLesson;
	}

	public List<TeacherLesson> getListTeacherLessons() {
		return listTeacherLessons;
	}
		
	public List<StudentLesson> getMyLessonList(Long id) {
		try {
			myLessonList = studentLessonService.myLessonList(id);
			if(myLessonList != null){
				return myLessonList;
			}else{
				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public Object getRowKey(StudentLesson object) {
		return null;
	}

	@Override
	public StudentLesson getRowData(String rowKey) {
		// TODO Auto-generated method stub
		return null;
	}

	public User getUser() {
		if(user == null){
			user = new User();
		}
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public Boolean isGetLesson() {
		return getLesson;
	}

	public void setGetLesson(Boolean getLesson) {
		this.getLesson = getLesson;
	}
}
